package com.lime49.lockcrypt;
import com.lime49.lockcrypt.ui.AbstractDialog;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

/**
* A form used for editing account fields
*/
public class FrmAddField extends AbstractDialog {
    private ResourceBundle lang;
    private Account account;
    private JPanel content;
    private JTextField txtFieldName;
    
    public FrmAddField(JDialog owner, Account account, String fieldName, boolean editing, ResourceBundle lang) {
        super(owner, true);
        this.lang = lang;
        this.account = account;
        if(editing) {
            setTitle(lang.getString("EDITFIELD"));
        } else {
            setTitle(lang.getString("ADDFIELD"));
        }
        KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        Action escapeAction = new AbstractAction() {
            // close the frame when the user presses escape
            public void actionPerformed(ActionEvent e) {
                result = JOptionPane.CANCEL_OPTION;
                dispose();
            }
        };
        this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
        this.getRootPane().getActionMap().put("ESCAPE", escapeAction);
        content = new JPanel(new GridBagLayout());
        content.setBorder(Constants.EMPTYBORDER);
        this.setContentPane(content);
        GridBagConstraints c = new GridBagConstraints();
        c.insets = Constants.GLOBALINSETS;
        c.weighty = 0.5;
        
        JLabel lblFieldName = new JLabel(lang.getString("FIELDNAME"));
        //final String oldname = fieldName;
            Utils.setgrid(c,0,0,1,1,GridBagConstraints.NONE,GridBagConstraints.WEST,0.5);
            content.add(lblFieldName,c);
        txtFieldName = new JTextField(20);
            lblFieldName.setLabelFor(txtFieldName);
            txtFieldName.setText(fieldName);
            Utils.setgrid(c,0,1,1,1,GridBagConstraints.REMAINDER,GridBagConstraints.CENTER,0.8);
            content.add(txtFieldName,c);
        JPanel buttonbox = new JPanel(new FlowLayout(FlowLayout.TRAILING));
            JButton btnSave = new JButton(lang.getString("SAVE"));
                if(fieldName.isEmpty()) {
                    btnSave.setText(lang.getString("ADD"));
                } else {
                    btnSave.setActionCommand("edit");
                }
                btnSave.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        if(validateField()) {
                            result = JOptionPane.OK_OPTION;
                            dispose();
                        }
                        //addField(e, oldname, txtFieldName.getText().trim());
                    }
                });
                JButton btncancel = new JButton(lang.getString("CANCEL"));
                btncancel.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                       dispose();
                   }
                });
                buttonbox.add(btnSave);
                buttonbox.add(btncancel);
                btnSave.setMinimumSize(Constants.MINSIZE);
                btnSave.setPreferredSize(Constants.PREFSIZE);
                btnSave.setMaximumSize(Constants.MAXSIZE);
                btncancel.setMinimumSize(Constants.MINSIZE);
                btncancel.setPreferredSize(Constants.PREFSIZE);
                btncancel.setMaximumSize(Constants.MAXSIZE);
                this.getRootPane().setDefaultButton(btnSave);
            Utils.setgrid(c,1,0,2,1,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1);
            c.insets = new Insets(0,0,0,0);
            content.add(buttonbox,c);
        
        this.pack();
        this.setLocationRelativeTo(owner);
        this.setVisible(true);
    }
    
    /**
    * Gets the name of the field being edited
    * @return The name of the field being edited
    */
    public String getFieldName() {
        return txtFieldName.getText().trim();
    }
    
    /**
    * Validates that the field name is not blank and does not exist in the account
    * @return True if the field is valid, otherwise false
    */
    private boolean validateField() {
        String newname = txtFieldName.getText().trim();
        if(newname.length() == 0) {
            JOptionPane.showMessageDialog(this,lang.getString("ERR_FIELDBLANK"),lang.getString("ERR_FIELDNAMEINVALID"), JOptionPane.ERROR_MESSAGE);
            return false;
        } else if(account.fieldExists(newname)) { // actionCommand is "add", and type exists
            JOptionPane.showMessageDialog(this,lang.getString("ERR_FIELDEXISTS"),lang.getString("ERR_FIELDNAMEINVALID"), JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }

}
